package edu.sctu.yueding.activitys;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.TypeReference;
import com.android.volley.Response;
import com.android.volley.VolleyError;

import java.util.ArrayList;
import java.util.List;

import edu.sctu.yueding.R;
import edu.sctu.yueding.adapter.MyTeamListViewAdapter;
import edu.sctu.yueding.bean.JsonBaseListBean;
import edu.sctu.yueding.bean.MyTeamMemberBean;
import edu.sctu.yueding.config.URLConfig;
import edu.sctu.yueding.utils.DateUtils;
import edu.sctu.yueding.utils.JumpUtils;
import edu.sctu.yueding.utils.ProgressUtil;
import edu.sctu.yueding.utils.httputils.GetVolleyHttp;
import edu.sctu.yueding.utils.httputils.SharePreference;
import edu.sctu.yueding.view.XListView;

/**
 * Created by 鑫爷您慢点 on 2016/8/2.
 */
public class MyTeamActivity extends BaseActivity implements XListView.IXListViewListener, AdapterView.OnItemClickListener {
    private XListView listView;
    private Activity mActivity = this;
    private TextView mBackTxt;

    private MyTeamListViewAdapter adapter;

    private SharePreference sharePreference;
    private List<MyTeamMemberBean> datas;

    private String token;

    private Handler handler = new Handler();
    private ProgressUtil progressUtil;
    private ProgressDialog progressDialog;

    private boolean isAddAll = true;
    private int page = 0;
    private Long matchId;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_team);
        initSystemBar(this, 0);
        initView();
    }

    private void initView() {

        mActivity = this;

        listView = (XListView) findViewById(R.id.id_my_team_list_view);
        listView.setPullLoadEnable(false);
        listView.setPullRefreshEnable(true);
        listView.setXListViewListener(this);
        listView.setOnItemClickListener(this);

        mBackTxt = (TextView) findViewById(R.id.id_my_team_back_txt);

        sharePreference = SharePreference.getInstance(this);

        mBackTxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        datas = new ArrayList<>();
        token = sharePreference.getString("token");

        progressUtil = ProgressUtil.getInstance(this, null);
        progressDialog = progressUtil.getProgressDialog(ProgressDialog.STYLE_SPINNER, true);

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {

                initData();
                progressUtil.dismiss(progressDialog);
            }

        }, 1000);


    }

    private void initData() {

        Intent intent = getIntent();
        matchId = intent.getLongExtra("matchId", 0l);

        if (matchId != 0l) {

            Log.e("matchId---", String.valueOf(matchId));

            GetVolleyHttp.getInstance(MyTeamActivity.this).getStringGETJsonData(URLConfig.getMyTeamsMember(matchId), new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {


                    try {

                        JsonBaseListBean<MyTeamMemberBean> jsonBaseListBean = JSON.parseObject(response,
                                new TypeReference<JsonBaseListBean<MyTeamMemberBean>>() {
                                }.getType());

                        if (jsonBaseListBean.getMeta().code == 200) {
                            datas = jsonBaseListBean.getData();

                            if (datas == null) {

                                Toast.makeText(mActivity, "暂无数据", Toast.LENGTH_SHORT).show();

                            } else {

                                initAdapter();
                            }
                        }

                    } catch (JSONException exception) {
                        Toast.makeText(mActivity, "网络请求失败", Toast.LENGTH_SHORT).show();
                    }
                    progressUtil.dismiss(progressDialog);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    progressUtil.dismiss(progressDialog);
                }
            }, "Bearer " + token);
        }
    }


    @Override
    public void onRefresh() {
        page = 0;
        isAddAll = false;
        initData();

    }

    @Override
    public void onLoadMore() {
        isAddAll = true;
        initData();
    }


    private void initAdapter() {

        if (adapter == null) {
            adapter = new MyTeamListViewAdapter(mActivity, datas);
            listView.setAdapter(adapter);
        } else {

            adapter.fresh(datas, isAddAll);
        }
        stop();

    }

    private void stop() {
        listView.stopLoadMore();
        listView.stopRefresh();
        listView.setRefreshTime(DateUtils.getTime());
    }


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

        MyTeamMemberBean memberBean = (MyTeamMemberBean) adapter.getItem(i - 1);

        Bundle b = new Bundle();
        b.putLong("id", memberBean.getId());
        b.putString("RealName", memberBean.getRealName());
        b.putString("Gender", memberBean.getGender());
        b.putString("IdNumber", memberBean.getIdNumber());
        b.putString("PhoneNumber", memberBean.getPhoneNumber());
        b.putLong("matchId", matchId);

        JumpUtils.jumpActivity(mActivity, UpdateMemberActivity.class, b);
        finish();

    }
}
